<?php 
/**
 * Invitation List.
 * 
 * @author james
 *
 */
class InvitationList extends Zend_Db_Table_Abstract {

	const PENDING = 'pending';
	const ACTIVE = 'active';
	/**
	 * The default table name 
	 */
	protected $_name = 'invitation_list';
	protected $_primary = 'id';
	
	protected $_referenceMap = array(
		'Invitation' => array(
			'columns' => 'invitation_id',
			'refTableClass' => 'Invitation',
			'refColumns' => 'id'
		),
		'InvitedUser' => array(
			'columns' => 'invited_user_id',
			'refTableClass' => 'User',
			'refColumns' => 'id'
		)
	);
	
	/**
	 * 
	 * @param $id
	 * @return return found invitationList
	 */
	public static function findById($id) {
		$invitationList = new InvitationList();
		return $invitationList->find($id)->current();
	}
	
	/**
	 * Get invitation.
	 * 
	 * @param $invitationList
	 */
	public static function getInvitation($invitationList) {
		return $invitationList->findParentRow('Invitation');
	}
	
	/**
	 * 
	 * @param $invitationList
	 * @return return invited user
	 */
	public static function getInvitedUser($invitationList) {
		return $invitationList->findParentRow('User','InvitedUser');
	}
	
	/**
	 * Find invitation user list by invitation id.
	 * 
	 * @param $invitation_id
	 * @return user list, invitaion list id as key and user object as value
	 */
	public function findByUserId($invitation_id) {
		$select = $this->select();
		$select->where('invitation_id = ?', $invitation_id);
		$row = $this->fetchAll($select);
		$userList = array();
		$userObj = new User();
		foreach ($row as $invitationList) {
			$invited_user_id = $invitationList->invited_user_id;
			$user = $userObj->findById($invited_user_id);
			if ($user) {
				$userList[$invitationList->id] = $user;
			}
		}
		return $userList;
	}
	
	/**
	 * Find unique entry by invitation id and invited user id
	 * 
	 * @param $invitation_id
	 * @param $invited_user_id
	 * @return return found result
	 */
	public function findUnique($invitation_id, $invited_user_id) {
		$select = $this->select();
		$select->where('invitation_id = ?', intval($invitation_id))
		->where('invited_user_id = ?', intval($invited_user_id));
		$row = $this->fetchRow($select);
		return $row;
	}
	
	/**
	 * Check if the given user id is been invited by
	 * @param $invitation
	 * @param $user_id
	 */
	public static function isInvited($invitation, $user_id) {
		
	}
	
	/**
	 * Add only new user to invitation list, if the user already in the list, then just return it.
	 * 
	 * @param $invitation_id
	 * @param $invited_user_id
	 * @param $status invitation list status, active - the user is been invited, 'pending' - user send approval request
	 * @return return id
	 */
	public function addNewUserToList($invitation_id, $invited_user_id, $status) {
		$invitationList = $this->findUnique($invitation_id, $invited_user_id);
		if (!$invitationList) {
			$sql_date_pattern = 'yyyy-MM-dd HH:mm:ss';
			$date_obj = new Zend_Date();
			$current_time = $date_obj->get($sql_date_pattern);
			$data = array(
				'invitation_id'=>intval($invitation_id),
				'invited_user_id'=>intval($invited_user_id),
				'status' => $status,
				'category'=>'',
				'created'=>$current_time
			);
			return $this->insert($data);
		} else {
			return $invitationList->id;
		}
	}
	
	/**
	 * Delete by id.
	 * 
	 * @param $id
	 */
	public function deleteById($id) {
		return $this->delete("id = $id");
	}
	
	/**
	 * Delete one row by given invitation id and user id.
	 * 
	 * @param $invitation_id
	 * @param $invited_user_id
	 */
	public function deleteByInvitationIdAndUserId($invitation_id, $invited_user_id) {
		$row = $this->findUnique($invitation_id, $invited_user_id);
		return $row->delete();
	}
	
	/**
	 * Approve invitation list.
	 * 
	 * @param $invitation_id
	 * @param $invited_user_id
	 */
	public function approveInvitation($invitation_id, $invited_user_id) {
		$row = $this->findUnique($invitation_id, $invited_user_id);
		$row->status = 'active';
		$row->save();
	}
}
?>